package hczc.longcai.com.hccrowdfunding.home.second_invest.activities;

import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.TextView;

import hczc.longcai.com.hccrowdfunding.R;
import hczc.longcai.com.hccrowdfunding.activity.BaseActivity;
import hczc.longcai.com.hccrowdfunding.application.BaseApplication;

public class PayActivity extends BaseActivity implements View.OnClickListener {

    private static final String TAG = "PayActivity";
    private View back;

    private TextView balance;
    private TextView fraction;
    private TextView money;


    private TextView pay;
    private PopupWindow popupWindow;
    private View main;
    private TextView popTitle;

    private int copies;
    private int each;

    private int c_balance;
    private int c_money;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pay);

        initMoney();
        initViews();
    }

    private void initMoney() {
        Intent intent = getIntent();
        Bundle extras = intent.getExtras();
        copies = extras.getInt("copies");
        each = extras.getInt("each");
    }

    private void initViews() {
        back = findViewById(R.id.head_back);
        back.setOnClickListener(this);
        ((TextView) findViewById(R.id.head_title)).setText("支付");
        findViewById(R.id.head_right_pic).setVisibility(View.GONE);
        main = findViewById(R.id.pay_activity_main);

        balance = ((TextView) findViewById(R.id.pay_balance));
        fraction = ((TextView) findViewById(R.id.pay_fraction));
        fraction.setText(String.valueOf(copies));
        money = ((TextView) findViewById(R.id.pay_money));
        money.setText(String.valueOf(each * copies));

        pay = ((TextView) findViewById(R.id.affirm_listview_consignee_qzf));
        pay.setText("支付");
        pay.setOnClickListener(this);

        initDatas();
        initPop();

    }

    private void initDatas() {
        Log.d(TAG, "copies = " + copies + "each = " + each);
//        String balance = this.balance.getText().toString();
//        String fraction = this.fraction.getText().toString();
//        String money = this.money.getText().toString();
//        c_balance = Integer.valueOf(balance);
//        c_fraction = Integer.valueOf(fraction);
//        c_money = Integer.valueOf(money);
        String a_balance = balance.getText().toString().trim();
        c_balance = Integer.valueOf(a_balance);
        c_money = copies * each;


    }

    private void initPop() {
        View view = LayoutInflater.from(this).inflate(R.layout.popupwindow_pay, null);
        BaseApplication.scaleScreenHelper.loadView(((ViewGroup) view));
        View button = view.findViewById(R.id.pay_button);
        popTitle = ((TextView) view.findViewById(R.id.pay_title));
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
            }
        });
        popupWindow = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
    }


    private boolean isAfford(int i, int j) {
        boolean flag = false;
        if (i >= j) {
            flag = true;
        }
        return flag;
    }

    @Override
    public void onClick(View view) {
        int index = view.getId();
        String title = "提示";
        switch (index) {
            case R.id.head_back:
                finish();
                break;

            case R.id.affirm_listview_consignee_qzf:
//                View dialogView = LayoutInflater.from(PayActivity.this).inflate(R.layout.pay_dialog,null);
                if (isAfford(c_balance, c_money)) {
                    title = "付款成功";
                    popTitle.setText(title);
                    popupWindow.showAtLocation(main, 0, 0, 0);
                } else {
                    title = "账户余额不足，请充值";
                    popTitle.setText(title);
                    popupWindow.showAtLocation(main, 0, 0, 0);
                }
//                    new AlertDialog.Builder(PayActivity.this)
//                            .setMessage(title)
//                            .setPositiveButton("确定",null)
//                            .show();
//                startActivity(new Intent(PayActivity.this,PayDialogActivity.class));
//                PayDialog dialog = new PayDialog(PayActivity.this);
//                dialog.show();

                break;
        }
    }
}
